package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import easier.log.logger.Logger;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.BackpressureStrategy;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.FlowableEmitter;
import io.reactivex.rxjava3.core.FlowableOnSubscribe;
import io.reactivex.rxjava3.core.FlowableSubscriber;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.functions.Predicate;
import io.reactivex.rxjava3.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscription;

public class MainActivity48 extends AppCompatActivity {

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity48.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main48 );

        test();
    }

    private void test() {

        Flowable.create( new FlowableOnSubscribe< String >() {

                @Override
                public void subscribe( @NonNull FlowableEmitter< String > emitter ) throws Throwable {

                    emitter.onNext( "1" );
                    emitter.onNext( "2" );
                    emitter.onNext( "3" );
                    emitter.onNext( "4" );
                    emitter.onNext( "5" );
                    //emitter.onComplete();

                }
            }, BackpressureStrategy.BUFFER )
            .take( 1 )
            .flatMap( new Function< String, Publisher< Long > >() {
                @Override
                public Publisher< Long > apply( String s ) throws Throwable {

                    return Flowable.interval( 1, TimeUnit.SECONDS );
                }
            } )
            .takeWhile( new Predicate< Long >() {
                @Override
                public boolean test( Long o ) throws Throwable {

                    return o <= 10;
                }
            } )
            .subscribeOn( Schedulers.computation() )
            .subscribe( new FlowableSubscriber< Object >() {

                private Subscription mSubscription;

                @Override
                public void onSubscribe( @NonNull Subscription s ) {

                    mSubscription = s;
                    s.request( Integer.MAX_VALUE );
                }

                @Override
                public void onNext( Object s ) {

                    Logger.d( s );
                }

                @Override
                public void onError( Throwable t ) {

                    Logger.d( t.getMessage() );
                }

                @Override
                public void onComplete() {

                    Logger.d( "complete" );
                }
            } );
    }
}